<?php
namespace PsiFramework\Classes\Util\DAO;

import("Classes.Util.DAO.IPrimitiveObjectDAO");
import("Classes.Util.UI.LanguageManager");
import("Classes.Util.UI.TemplateManager");

use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;
use \PsiFramework\Classes\Util\DAO\IPrimitiveObjectDAO;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\UI\LanguageManager;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 *
 *
 * @package DAO
 */
class DateDAO implements IPrimitiveObjectDAO
{

    protected static $_singleton = null;

    public function parse($value=null, $options=null)
    {

    }

    public function edit(
        IPrimitiveObject $object=null,
        $name=null,
        $options=null
    )
    {

    }

    public function display(IPrimitiveObject $object=null, $options=null)
    {

    }

    public function getJavascriptCode($selecter, $option='')
    {
        $js =  '$(\''.$selecter.'\').datepicker('.$option.
               //        '{'.
               //            'dateFormat: "'.
               //                $localisedConfig->datePicker->dateFormat.
               //            '"'.
               //        '}'.
               ');';
        
        return $js;
    }
    
    public function javascript(IPrimitiveObject $object=null, $options=null)
    {
        $languageManager = LanguageManager::getInstance();
        $localisedConfig = $languageManager->getLocalisedConfig();

        $js  = '$(document).ready(function() {'."\n".
               //'    $.validator.addMethod('."\n".
               //'        "'.$languageManager->getLanguage().'Date",'."\n".
               //'        function(value, element) {'."\n".
               //'            return value.match('.
               //                $localisedConfig->datePicker->dateRegex.
               //             ') || value == "";'."\n" .
               //'        },'."\n".
               //'        "'.$localisedConfig->datePicker->dateMessage.'"'."\n".
               //'    );'."\n" .
               //'    $.datepicker.regional["'.
               //        $languageManager->getLanguage().
               //     '"] ;'."\n".
               $this->getJavascriptCode('.psiDate')."\n".
               '});'."\n" ;

        $templateManager = TemplateManager::getInstance();
        $templateManager->addRemoteScript(
            'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        );
        $templateManager->addRemoteScript(
            'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js'
        );
        $templateManager->addRemoteScript(
            './Scripts/jquery.validate.js'
        );

        if ($languageManager->getLanguage() != 'en') {
            $templateManager->addFileScript(
                'Applications/DefaultApp/Template/scripts/jquery.ui.datepicker-'.
                $languageManager->getLanguage().'.js'
            );
        }

        $templateManager->addLitteralScript($js);
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return     DateDAO instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new DateDAO();
        }

        return self::$_singleton;
    }
}
